using System;
using System.Collections.Generic;
using System.Text;
using WindowsLive.Writer.Api;
using System.Windows.Forms;
using System.Drawing;
using ScottIsAFool.Common;

namespace InsertVideo
{
    [WriterPlugin("53357c8b-5919-4e32-8c25-305d27c17a37", "Insert Video",
        Description = "Insert a video into your blog entry",
        HasEditableOptions = true,
        Name = "Insert Video",
        PublisherUrl = "http://scottisafooldev.spaces.live.com",
        ImagePath="icon.gif")]
    [InsertableContentSource("Video")]
    [UrlContentSource("http://",
        ProgressMessage = "Processing",
        ProgressCaption = "Getting Video Details",
        RequiresProgress = true,
        UrlPattern = @"https?://(www\.)?((video\.google|youtube|soapbox\.msn|vids\.myspace|video\.yahoo|muveemix|brightcove|divshare)\.com|myvideo\.de)")]

    public class InsertAVideo : SmartContentSource
    {
        VideoSettings _defaultsettings;
               
        public InsertAVideo()
        { }

        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);
            _defaultsettings = new VideoSettings(pluginOptions);
        }

        public override void EditOptions(IWin32Window dialogOwner)
        {
            VideoSettings settings = new VideoSettings(this.Options);
            frmOptions ops = new frmOptions(settings);                            
            ops.ShowDialog();            
        }

        public override DialogResult CreateContent(IWin32Window dialogOwner, ISmartContent newContent)
        {
            VideoSettings settings = new VideoSettings(newContent.Properties);
            settings.DefaultOption = _defaultsettings.DefaultOption;
            settings.DefaultWidth = _defaultsettings.DefaultWidth;
            settings.IsEnabled = _defaultsettings.IsEnabled;
            settings.Transparency = _defaultsettings.DefaultTransparancySetting;
            settings.Preview = _defaultsettings.Preview;
            settings.OpenNew = _defaultsettings.DefaultOpen;
            settings.TitleIsEnabled = _defaultsettings.DefaultTitleIsEnabled;
            settings.Autoplay = _defaultsettings.DefaultAutoplay;
            using (frmMain main = new frmMain(settings))
            {
                DialogResult result = main.ShowDialog();
                if (result == DialogResult.OK)
                {
                    try
                    {
                        GenerateHTML html = new GenerateHTML(newContent);
                        html.GenerateHTMLCode();
                    }
                    catch
                    {
                        MessageBox.Show("Sorry, the URL you have used hasn't got a video attached to it.", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                return result;
            }
        }

        public override void CreateContentFromUrl(string url, ref string title, ISmartContent newContent)
        {

            VideoSettings settings = new VideoSettings(newContent.Properties);
            GetVideoCode vc = new GetVideoCode(settings.IsEnabled, settings.DefaultWidth);
            settings.Option = _defaultsettings.DefaultOption;
            settings.DefaultWidth = _defaultsettings.DefaultWidth;
            settings.IsEnabled = _defaultsettings.IsEnabled;
            settings.Preview = _defaultsettings.Preview;
            settings.Transparency = _defaultsettings.DefaultTransparancySetting;
            settings.OpenNew = _defaultsettings.DefaultOpen;
            settings.TitleIsEnabled = _defaultsettings.DefaultTitleIsEnabled;
            settings.Provider = vc.VideoProvider(url);
            settings.Embed = vc.Code;
            settings.Width = vc.GetWidth;
            settings.Link = url;
            settings.Id = vc.GetID;
            settings.Ratio = vc.GetRatio;
            settings.Title = vc.GetHtmlPageSource;
            settings.Autoplay = _defaultsettings.DefaultAutoplay;

            try
            {
                GenerateHTML ghtml = new GenerateHTML(newContent);
                ghtml.GenerateHTMLCode(); 
                
            }
            catch
            {
                MessageBox.Show("Sorry, the URL you have used hasn't got a video attached to it.", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public override string GeneratePublishHtml(ISmartContent content, IPublishingContext publishingContext)
        {

            VideoSettings _settings = new VideoSettings(content.Properties);
            GenerateHTML ghtml = new GenerateHTML(content);
            content.Layout.TopMargin = _settings.Top;
            content.Layout.RightMargin = _settings.Right;
            content.Layout.BottomMargin = _settings.Bottom;
            content.Layout.LeftMargin = _settings.Left;
            int width = _settings.Width;
            int option = _settings.Option;
            string embed = _settings.FinalEmbedCode;
            string link = _settings.Link;
            string code = "There was an error getting your video.";
            string newwindow = "";
            string title = "";
            if (_settings.OpenNew)
                newwindow = " target=\"_blank\"";
            if (_settings.Provider != "muveemix")
            {
                if (_settings.TitleIsEnabled)
                    title = "\n<br />\n<a href=\"" + link + "\"" + newwindow + ">" + _settings.Title + "</a>";
            }
            if ((content.Files != null) && (content.Files.Filenames != null) && (content.Files.Filenames.Length > 0))
            {
                switch (option)
                {
                    case 0:
                        code = embed;
                        code += title;
                        break;
                    case 1:
                        code = String.Format("<a href=\"{1}\"{2}><img border=\"0\" src=\"{0}\" /></a>", content.Files.GetUri(content.Files.Filenames[0]), link, newwindow);
                        code += title;
                        break;
                    case 2:
                        AdaptiveHtmlObject html = new AdaptiveHtmlObject(embed, content.Files.GetUri(content.Files.Filenames[0]).ToString(), link);
                        html.OpenPreviewInNewWindow = _settings.OpenNew;
                        code = html.GenerateHtml(HtmlType.AdaptiveHtml);
                        code += title;
                        break;
                }
            }

            return code;
        }
        
        public override ResizeCapabilities ResizeCapabilities
        {
            get
            {
                return ResizeCapabilities.Resizable | ResizeCapabilities.PreserveAspectRatio;
            }
        }

        public override SmartContentEditor CreateEditor(ISmartContentEditorSite editorSite)
        {
            return new ContentEditor();
        }

        public override void OnResizeComplete(ISmartContent content, Size newSize)
        {
            VideoSettings settings = new VideoSettings(content.Properties);           
            settings.Width = newSize.Width;

            GenerateHTML ghtml = new GenerateHTML(content);
            ghtml.GenerateHTMLCode(); 
            base.OnResizeComplete(content, newSize);
        }

        public override void OnResizeStart(ISmartContent content, ResizeOptions options)
        {
            VideoSettings tempSettings = new VideoSettings(content.Properties);
            options.AspectRatio = (double)tempSettings.Ratio;
            base.OnResizeStart(content, options);
        }
    }
}
